﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Xml.Serialization;
using System.IO;
using Intorise.Common;

namespace Model
{
    public class FightLocation
    {
        public string MapName { get; set; }
        public string LocationName { get; set; }
        public List<Point> LocationPoints = new List<Point>();

        public static List<FightLocation> Load(string fileName = "Location.xml")
        {
            fileName = AppDomain.CurrentDomain.BaseDirectory + "\\" + fileName;
            XmlSerializer serializer = new XmlSerializer(typeof(List<FightLocation>));
            FileStream stream = null;
            if (!File.Exists(fileName))
            {
                List<FightLocation> list = new List<FightLocation>();
                var location = new FightLocation { MapName = "天空之城", LocationName = "170级怪" };
                location.LocationPoints.Add(new Point(250, 250));
                list.Add(location);
                Save(list, fileName);
            }
            try
            {
                stream = File.Open(fileName, FileMode.Open);
                return (List<FightLocation>)serializer.Deserialize(stream);
            }
            catch (Exception ex)
            {
                ConsoleBLL.WriteLineError("加载路径数据出错！\r\n" + ex.Message);
                return new List<FightLocation>(0);
            }
            finally
            {
                if (stream != null) { stream.Close(); }
            }
        }

        protected static void Save(List<FightLocation> list, string fileName)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<FightLocation>));
            FileStream stream = null;
            try
            {
                stream = File.Open(fileName, FileMode.Create);
                serializer.Serialize(stream, list);
            }
            catch (Exception ex)
            {
                ConsoleBLL.WriteLineError("保存路径数据出错！\r\n" + ex.Message);
            }
            finally
            {
                if (stream != null) { stream.Close(); }
            }
        }
    }
}
